package tianwang.utils.dict;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import tianwang.dao.WordLevelDao;
import tianwang.model.WordsDaoEntry;
/**
 * 从数据库中取得单词级别的词典类
 */
public class LevelDaoDict implements Dictionary {

	// 从数据库中提取单词的级别
	private WordLevelDao dao = null;
	
	public LevelDaoDict(){
		this.dao = new WordLevelDao();
	}
	
	public boolean contains(String entry){
		try {
			WordsDaoEntry daoEntry = dao.getWord(entry.toString());
			if(daoEntry != null)
				return true;
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return false;
	}

	public void initial() {
	}

	public Set<String> listCoreWords() {
		return null;
	}

	public Collection<Entry> listEntry() {
		return null;
	}

	public Entry lookup(String entry) {
		Entry result = null;
		
		try {
			WordsDaoEntry daoEntry = dao.getWord(entry.toString());
			if(daoEntry != null){
				result = new Entry();
				Map<String,String> items = new HashMap<String,String>();
				items.put(Entry.CORE_WORD, daoEntry.getWord());
				items.put("level", daoEntry.getLevel()+"");
				result.setItems(items);
			}
			return result;
				
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static void main(String[] a){
		LevelDaoDict dict = new LevelDaoDict();
		Entry en = dict.lookup("a");
		System.out.println(en.getContent("level"));
	}

}
